package com.jackchong.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;

import androidx.annotation.FloatRange;
import androidx.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;

import com.jackchong.utils.JAuto;;
import com.jackchong.utils.ViewUtils;
import com.utils.R;


/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2019/1/3 17:17
 */

public class RectProgressBar extends JView {

    private int mStartColor;
    private int mEndColor;
    private float mProgress;
    private int mBgColor;
    private Paint mPaint;
    private Paint mBgPaint;
    private TextPaint mTextPaint;
    private Paint mStrokePaint;

    public RectProgressBar(Context context) {
        this(context, null);

    }

    public RectProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RectProgressBar);
        parseAttrs(typedArray);
        typedArray.recycle();

        init();
    }

    private void parseAttrs(TypedArray typedArray) {
        mStartColor = typedArray.getColor(R.styleable.RectProgressBar_JStartColor, Color.BLACK);
        mEndColor = typedArray.getColor(R.styleable.RectProgressBar_JEndColor, Color.WHITE);
        mBgColor = typedArray.getColor(R.styleable.RectProgressBar_JBackground, Color.RED);
        mProgress = typedArray.getFloat(R.styleable.RectProgressBar_JProgress, 0);
        mProgress = mProgress < 0 ? 0 : mProgress > 5 ? 5 : mProgress;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mPaint.setShader(new LinearGradient(0, 0, w, 0, mStartColor, mEndColor, Shader.TileMode.MIRROR));
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);
        mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBgPaint.setAntiAlias(true);
        mBgPaint.setColor(mBgColor);

        mStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mStrokePaint.setAntiAlias(true);
        mStrokePaint.setStyle(Paint.Style.STROKE);
        mStrokePaint.setStrokeWidth(2);
        mStrokePaint.setColor(0xFFE6E6E6);

        mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setAlpha(Byte.MAX_VALUE);
        mTextPaint.setTextSize(JAuto.auto(24));
    }

    public void setProgress(@FloatRange(from = 0, to = 5) float progress){
        mProgress = progress < 0 ? 0 : progress > 5 ? 5 : progress;
        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mProgress == 0){
            canvas.drawRect(0, 0, getWidth(), getHeight(), mStrokePaint);
            canvas.drawText("暂无评分", getWidth() / 2,  ViewUtils.getTextCenterY(getHeight() / 2, mTextPaint), mTextPaint);
        }

        int right = (int) (getWidth() * mProgress / 5);
        canvas.drawRect(0, 0, right, getBottom(), mBgPaint);
        canvas.drawRect(0, 0, right, getBottom(), mPaint);
    }

}
